﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Model.Sec
{
    public class UserGroup
    {
        #region UserGroup的属性
        private string _UserGroupISN;
        /// <summary>
        /// 用户组ISN
        /// </summary>
        public string UserGroupISN
        {
            get { return _UserGroupISN; }
            set { _UserGroupISN = value; }
        }

        private string _UserGroupName;
        /// <summary>
        /// 用户组名
        /// </summary>
        public string UserGroupName
        {
            get { return _UserGroupName; }
            set { _UserGroupName = value; }
        }
        private string _SortIndex;
        /// <summary>
        /// 显示顺序
        /// </summary>
        public string SortIndex
        {
            get { return _SortIndex; }
            set { _SortIndex = value; }
        }
        private string _Description;
        /// <summary>
        /// 组描述
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        private DateTime _CreateTime;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime
        {
            get { return _CreateTime; }
            set { _CreateTime = value; }
        }
        private int _isroots;
        /// <summary>
        /// 是否一般用户组可见
        /// </summary>
        public int ISROOTS
        {
            set { _isroots = value; }
            get { return _isroots; }
        }

        /// <summary>
        /// 此用户组的权限
        /// </summary>
        private OperateCollection _Operate;
        public OperateCollection Operates
        {
            get
            {
                return this._GetOperate();

            }
        }
        /// <summary>
        /// 隶属于此组的用户
        /// </summary>
        private UserCollection _User;
        public UserCollection Users
        {
            get
            {

                return this._GetUsers();

            }
        }
        #endregion

        /// <summary>
        /// 组包含的用户
        /// </summary>
        /// <returns></returns>
        private UserCollection _GetUsers()
        {
            //List<User> list = new List<User>();
            string sql = "SELECT SU.* FROM SEC_USERINFO_new SU,SEC_USERGROUP_USERINFO SUU WHERE SU.USERISN=SUU.USERISN AND SUU.USERGROUPISN='" + this.UserGroupISN + "'";
            System.Data.DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            UserCollection uc = new UserCollection(dt.Rows.Count);
            for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
            {
                Model.Sec.User user = new Model.Sec.User();
                if (dt.Rows[rowNum]["CREATETIME"].ToString() != "")
                    user.CreateTime = Convert.ToDateTime(dt.Rows[rowNum]["CREATETIME"]);
                user.EMail = dt.Rows[rowNum]["EMAIL"].ToString();
                user.IPSection = dt.Rows[rowNum]["IPSECTION"].ToString();
                user.Password = dt.Rows[rowNum]["PASSWORD"].ToString();
                user.Position = dt.Rows[rowNum]["POSITION"].ToString();
                user.Sort = dt.Rows[rowNum]["SORT"] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[rowNum]["SORT"]);
                user.Tel = dt.Rows[rowNum]["TEL"].ToString();
                user.UnitISN = dt.Rows[rowNum]["UNITISN"].ToString();
                user.UnitName = dt.Rows[rowNum]["UNITNAME"].ToString();
                user.UserISN = dt.Rows[rowNum]["USERISN"].ToString();
                user.UserName = dt.Rows[rowNum]["USERNAME"].ToString();

                uc.Users[rowNum] = user;
            }
            return uc;
        }
        /// <summary>
        /// 组包含的操作权限
        /// </summary>
        /// <returns></returns>
        private OperateCollection _GetOperate()
        {
            string sql = "select * from sec_Operate,sec_UserGroup_Operate SUO where SUO.OperateID=sec_Operate.OperateID and SUO.OperateIsn='" + this.UserGroupISN + "'";
            DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            OperateCollection oc = new OperateCollection(dt.Rows.Count);
            for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
            {
                Model.Sec.Operate operate = new Model.Sec.Operate();
                operate.BelongSystem = dt.Rows[rowNum]["BELONGSYSTEM"].ToString();
                //operate.Children = dt.Rows[rowNum]["CHILDREN"].ToString();
                operate.OperateID = dt.Rows[rowNum]["OPERATE"].ToString();
                operate.OperateName = dt.Rows[rowNum]["OPERATENAME"].ToString();
                operate.OperateType = dt.Rows[rowNum]["OPERATETYPE"].ToString();
                operate.OperateURL = dt.Rows[rowNum]["OPERATEURL"].ToString().Split(new char[';']);
                operate.ParentOperateID = dt.Rows[rowNum]["PARENTOPERATEID"].ToString();
                operate.PropertyInitialized = bool.Parse(dt.Rows[rowNum]["PROPERTYINITIALIZED"].ToString());
                operate.TreeCataLog = dt.Rows[rowNum]["TREECATALOG"].ToString();
                operate.TreeOrder = dt.Rows[rowNum]["TREEORDER"].ToString();
                oc.operates[rowNum] = operate;

            }
            return oc;
        }
    }
}
